package com.zsgeek.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.StringUtils;
import com.zsgeek.dao.Task_reviewDao;
import com.zsgeek.entity.Task_review;

public class Task_reviewServlet extends HttpServlet {

	private static final long serialVersionUID = 3443131191730737086L;

	private PrintWriter outWriter;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		outWriter = resp.getWriter();
		String m = req.getParameter("m");
		String order_no = req.getParameter("orderNo");
		System.out.println("order_no==========" + order_no);
		String review_type = req.getParameter("type");
		if (m == null) {
			list(req, resp, order_no, review_type);
		} else if ("save".equals(m)) {
			save(req, resp, order_no, review_type);
		} else if ("delete".equals(m)) {
			delete(req, resp);
		}
	}

	/**
	 * 保存（新增、修改）
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void save(HttpServletRequest req, HttpServletResponse resp,
			String order_no, String review_type) throws ServletException,
			IOException {

		String id = req.getParameter("id");
		String star = req.getParameter("star");
		String title = req.getParameter("title");
		String content = req.getParameter("content");
//		String review_type = req.getParameter("type");
        System.err.println("id=================" + id);
		if (!StringUtils.isNullOrEmpty(id)) {
			// 修改
			Task_review task_review = Task_reviewDao.getReview(Integer
					.parseInt(id));
			if (task_review != null) {
				task_review.setReview_star(Integer.parseInt(star));
				task_review.setReview_title(title);
				task_review.setReview_content(content);
				Task_reviewDao.update(task_review);

			}

		} else {
			String date = req.getParameter("d");// 所属日期
			// 新增
			Task_review task_review = new Task_review();
			task_review.setReview_star(Integer.parseInt(star));
			task_review.setReview_title(title);
			task_review.setReview_content(content);
			task_review.setOrder_no(order_no);
			task_review.setReview_type(Integer.parseInt(review_type));
			task_review.setCreated_time(date);
			Task_reviewDao.add(task_review);

		}
		list(req, resp, order_no, review_type);
	}

	/**
	 * 删除
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void delete(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String id = req.getParameter("id");
		if (!StringUtils.isNullOrEmpty(id)) {
			Task_reviewDao.delete(Integer.parseInt(id));
			// 删除成功

			String order_no = req.getParameter("orderNo");
			String review_type = req.getParameter("type");
			list(req, resp, order_no, review_type);
		}

	}

	/**
	 * 列表
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void list(HttpServletRequest req, HttpServletResponse resp,
			String order_no, String review_type) throws ServletException,
			IOException {
		String date = req.getParameter("d");
		List<Task_review> list = Task_reviewDao.getList(order_no,
				Integer.parseInt(review_type), date);
		StringBuilder str = new StringBuilder();
		/*
		 * <table class="usertablelist" style="width: 400px;"> <thead> <tr> <th
		 * style="text-align: center; width: 90px;">REVIEW星级</th> <th
		 * style="text-align: center; width: 90px;">REVIEW标题</th> <th
		 * style="text-align: center; width: 90px;">REVIEW内容</th> <th
		 * style="text-align: center; width: 105px;">操作</th> </tr> </thead>
		 * <tbody id="tdReview"> </tbody> </table>
		 */
		if(!StringUtils.isNullOrEmpty(review_type) && "1".equals(review_type)){
			if (list.size() > 0) {
				str.append("<table class=\"usertablelist\" style=\"width: 400px;\">");
				str.append("<thead><tr><th style=\"text-align: center; width: 90px;\">REVIEW星级</th>"
						+ "<th style=\"text-align: center; width: 90px;\">REVIEW标题</th>"
						+ "<th style=\"text-align: center; width: 90px;\">REVIEW内容</th>"
						+ "<th style=\"text-align: center; width: 105px;\">操作</th>"
						+ "</tr></thead>");
				str.append("<tbody>");
			}
			for (Task_review task_review : list) {
				str.append("<tr>");
				str.append("<td style=\"text-align: center;\">"
						+ task_review.getReview_star() + "</td>");
	
				str.append("<td style=\"text-align: center;word-break: break-word;word-wrap: break-word;\">"
						+ task_review.getReview_title() + "</td>");
				str.append("<td style=\"text-align: center;word-break: break-word;word-wrap: break-word;\">"
						+ task_review.getReview_content() + "</td>");
				str.append("<td style=\"text-align: center;\"><a href=\"javascript:info('"
						+ task_review.getId()
						+ "','"
						+ task_review.getReview_title()
						+ "','"
						+ task_review.getReview_content()
						+ "','"
						+ task_review.getReview_star()
						+ "')\" class=\"tablelink\">【修改】</a>"
						+ "<a href=\"javascript:del('"
						+ task_review.getId()
						+ "','"
						+ task_review.getOrder_no()
						+ "','"
						+ task_review.getCreated_time()
						+ "','"
						+ task_review.getReview_type()
						+ "');\" class=\"tablelink\" onclick=\"if(confirm('确定是否要删除此项记录?')){return true;}else{return false;}\">【删除】</a></td>");
	
				str.append("</tr>");
	
			}
			if (list.size() > 0) {
				str.append("</tbody>");
				str.append("</table>");
			}
			outWriter.print(str.toString()+"&&&&&&"+(list.size()==0?"":list.size()+""));
		}else{
			if (list.size() > 0) {
				str.append("<table class=\"usertablelist\" style=\"width: 430px;\">");
				str.append("<thead><tr><th style=\"text-align: center; width: 90px;\">FEEDBACK星级</th>"
						+ "<th style=\"text-align: center; width: 90px;\">FEEDBACK内容</th>"
						+ "<th style=\"text-align: center; width: 105px;\">操作</th>"
						+ "</tr></thead>");
				str.append("<tbody>");
			}
			for (Task_review task_review : list) {
				str.append("<tr>");
				str.append("<td style=\"text-align: center;\">"
						+ task_review.getReview_star() + "</td>");
	
				str.append("<td style=\"text-align: center;word-break: break-word;word-wrap: break-word;\">"
						+ task_review.getReview_content() + "</td>");
				str.append("<td style=\"text-align: center;\"><a href=\"javascript:infofd('"
						+ task_review.getId()
						+ "','"
						+ task_review.getReview_title()
						+ "','"
						+ task_review.getReview_content()
						+ "','"
						+ task_review.getReview_star()
						+ "')\" class=\"tablelink\">【修改】</a>"
						+ "<a href=\"javascript:delfd('"
						+ task_review.getId()
						+ "','"
						+ task_review.getOrder_no()
						+ "','"
						+ task_review.getCreated_time()
						+ "','"
						+ task_review.getReview_type()
						+ "');\" class=\"tablelink\" onclick=\"if(confirm('确定是否要删除此项记录?')){return true;}else{return false;}\">【删除】</a></td>");
	
				str.append("</tr>");
	
			}
			if (list.size() > 0) {
				str.append("</tbody>");
				str.append("</table>");
			}
			outWriter.print(str.toString()+"&&&&&&"+(list.size()==0?"":list.size()+""));
		}

	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
}
